package com.my.controller;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.my.pojo.Business;
import com.my.pojo.Goods;
import com.my.pojo.Person;
import com.my.service.IPersonService;
import com.my.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/person")
public class PersonController {
    IPersonService personService;

    @Autowired
    public void setPersonService(IPersonService personService) {
        this.personService = personService;
    }

    RedisTemplate<String, String> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private void putMessage(Map<String, String> map, String code, String message) {
        map.put("code", code);
        map.put("message", message);
    }

    private String PersonToJSON(Person person) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("birth", person.getBirth().toString());
        jsonObject.addProperty("name", person.getName());
        jsonObject.addProperty("sex", person.getSex());
        return jsonObject.toString();
    }

    /**
     * 展示个人信息
     *
     * @param req   header中存token
     * @return
     *      状态码+信息
     *          500     token错误
     *          501     未找到Person
     *          200     成功
     */
    @RequestMapping("/show")
    public String showPerson(HttpServletRequest req) {
        Map<String, String> map = new HashMap<>();
        String token = req.getHeader("token");
        Integer userId = JWTUtils.getId(token);

        if(userId == null) {
            putMessage(map, "500", "token错误");
        } else {
            Person person = personService.showPerson(userId);
            if(person == null) {
                putMessage(map, "501", "未找到Person");
            } else {
                putMessage(map, "200", "查找成功");
                map.put("person", PersonToJSON(person));
            }
        }
        return new Gson().toJson(map);
    }


    /**
     * 更新用户信息
     *
     * @param name 姓名
     * @param birth 生日（yyyy-MM-dd）
     * @param sex   性别(男/女)
     * @param req   header中存放token
     * @return
     *      状态码+信息
     *          500     日期格式错误
     *          501     用户不存在
     *          502     sql错误
     *          200     成功
     *
     */
    @RequestMapping("/update")
    public String updatePerson(String name, String birth, String sex, HttpServletRequest req) {
        Map<String, String> map = new HashMap<>();
        name = name.trim();
        sex = sex.trim();
        if(!sex.equals("男") && sex.equals("女")) {
            sex = "男";
        }
        Date birthday;
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        try {
            birthday = f.parse(birth);
        } catch (ParseException e) {
            putMessage(map, "500", "日期格式错误");
            return new Gson().toJson(map);
        }

        Integer userId = JWTUtils.getId(req.getHeader("token"));
        if(userId == null) {
            putMessage(map, "501", "用户不存在");
        } else  {

            Person person = new Person();
            person.setBirth(birthday);
            person.setName(name);
            person.setSex(sex.charAt(0));
            person.setStatus(0);

            Integer res = personService.updatePerson(person, userId);
            if(res == -1) {
                putMessage(map, "502", "sql错误");
            } if(res == -2) {
                putMessage(map, "503", "用户不存在");
            } else {
                putMessage(map, "200", "成功");
            }
        }
        return new Gson().toJson(map);
    }

}
